package stujdbc;

import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.util.IllegalFormatException;

public class Client extends JFrame {

    private static final String HOST_NAME = "127.0.0.1";

    public static void main(String[] args) {
        JFrame client = new Client();
    }
    private JTextField fieldId = new JTextField();
    private JTextField fieldName = new JTextField();
    private JTextField fieldGraduate = new JTextField();
    private JTextField fieldDepartment = new JTextField();
    private JTextField fieldEmail = new JTextField();
    private JTextField fieldPhone = new JTextField();
    private JButton buttonCreate = new JButton("Create");
    private JButton buttonSearch = new JButton("Search");

    public Client() {
        JPanel p0 = new JPanel();
        p0.add(new JLabel("Enter your student ID to create/search information"));

        JPanel p1 = new JPanel(new GridLayout(6, 2));
        p1.add(new JLabel("StudentImpl ID"));
        p1.add(fieldId);
        p1.add(new JLabel("Name"));
        p1.add(fieldName);
        p1.add(new JLabel("Graduate School"));
        p1.add(fieldGraduate);
        p1.add(new JLabel("Department"));
        p1.add(fieldDepartment);
        p1.add(new JLabel("Email"));
        p1.add(fieldEmail);
        p1.add(new JLabel("Phone Number"));
        p1.add(fieldPhone);

        JPanel p2 = new JPanel();
        p2.add(buttonCreate);
        p2.add(buttonSearch);

        add(p0, BorderLayout.NORTH);
        add(p1, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);

        buttonCreate.addActionListener(new CreateButtonListener());
        buttonSearch.addActionListener(new SearchButtonListener());

        pack();
        setTitle("Client");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private class CreateButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                int id = Integer.parseInt(fieldId.getText().trim());
                String name = fieldName.getText().trim();
                String graduate = fieldGraduate.getText().trim();
                String department = fieldDepartment.getText().trim();
                
                String email = fieldEmail.getText().trim();
                if (!email.contains("@")) 
                    throw new FormatIllegalException("Illegal Email address: "
                        + "Email address must contains '@' sign!");
                
                String phone = fieldPhone.getText().trim();
                
                if (!phone.matches("\\d+"))
                    throw new FormatIllegalException("Illegal Phone number: "
                        + "Phone number only contains numbers (0-9)!");

                
                Registry registry = LocateRegistry.getRegistry(HOST_NAME);
                Database db = (Database) registry.lookup("student");
                //
                // DONE:
                //
                Student student = db.create();
                student.setId(id);
                student.setName(name);
                student.setGraduate(graduate);
                student.setDepartment(department);
                student.setEmail(email);
                student.setPhone(phone);

                db.register(student);

            }
            catch (FormatIllegalException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
            catch (RemoteException ex) {
                ex.printStackTrace();
            }
            catch (NotBoundException ex) {
                ex.printStackTrace();
            }
        }
    }

    private class SearchButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            int id = Integer.parseInt(fieldId.getText().trim());

            //
            // DONE:
            //
            try {
                Registry registry = LocateRegistry.getRegistry(HOST_NAME);
                Database db = (Database) registry.lookup("student");
                //
                // DONE:
                //

                Student student = db.search(id);
                
                if (student == null)
                    throw new StudentNotExistException(
                        "Student " + id + " does NOT exist!");

                JOptionPane.showMessageDialog(null, "Student " + student.getId() + ": \n"
                    + "\t Name = " + student.getName() + "\n"
                    + "\t Graduate School = " + student.getGraduate() + "\n"
                    + "\t Department = " + student.getDepartment() + "\n"
                    + "\t Email = " + student.getEmail() + "\n"
                    + "\t Phone = " + student.getPhone() + "\n");
                System.out.println("Student " + student.getId() + ": \n"
                    + "\t Name = " + student.getName() + "\n"
                    + "\t Graduate School = " + student.getGraduate() + "\n"
                    + "\t Department = " + student.getDepartment() + "\n"
                    + "\t Email = " + student.getEmail() + "\n"
                    + "\t Phone = " + student.getPhone() + "\n");

            }
            catch (StudentNotExistException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
            catch (RemoteException ex) {
                ex.printStackTrace();
            }
            catch (NotBoundException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    private class StudentNotExistException extends Exception {
        public StudentNotExistException(String msg) {
            super(msg);
        }
    }
    
    private class FormatIllegalException extends Exception {
        public FormatIllegalException(String msg) {
            super(msg);
        }
    }
}
